var redArray = new Array();//非胆拖情况下记录红球列表
var blueArray = new Array();//非胆拖情况下记录蓝球列表
var redArray_dan = new Array();//胆拖情况下记录必出红球列表
var redArray_tuo = new Array();//胆拖情况下记录可能出红球列表
var blueArray_dan = new Array();//胆拖情况下记录必出蓝球列表
var blueArray_tuo = new Array();//胆拖情况下记录可能出蓝球列表
var phase_num;


$(document).ready(function () {

    $("span").click(function () {
        if ($(this).parent()[0].className == 'article_red_ball') {
            if ($(this).hasClass("red")) {
                $(this).removeClass("red");
                redArray.remove($(this).text());

            } else {
                $(this).addClass("red");
                redArray.push($(this).text());

            }
        }

        if ($(this).parent()[0].className == 'article_red_dan_ball') {

            if ($(this).hasClass("red")) {
                $(this).removeClass("red");
                redArray_dan.remove($(this).text());

            } else {
                if (redArray_dan.length == 4) {
                    alert('红球胆码最多可选4个');
                    return;
                }
                $(this).addClass("red");
                redArray_dan.push($(this).text());
                clearConflictTuoRed($(this).text());
            }
        }

        if ($(this).parent()[0].className == 'article_red_tuo_ball') {
            if ($(this).hasClass("red")) {
                $(this).removeClass("red");
                redArray_tuo.remove($(this).text());

            } else {
                $(this).addClass("red");
                redArray_tuo.push($(this).text());
                clearConflictDanRed($(this).text());

            }
        }

        if ($(this).parent()[0].className == 'aside_blue_ball') {

            if ($(this).hasClass("blue")) {
                $(this).removeClass("blue");
                blueArray.remove($(this).text());
            } else {

                $(this).addClass("blue");
                blueArray.push($(this).text());

            }
        }

        if ($(this).parent()[0].className == 'aside_blue_dan_ball') {
            if ($(this).hasClass("blue")) {
                $(this).removeClass("blue");
                blueArray_dan.remove($(this).text());
            } else {
                if (blueArray_dan.length == 1) {
                    alert('蓝球胆码最多一个');
                    return;
                }
                $(this).addClass("blue");
                blueArray_dan.push($(this).text());
                clearConflictTuoBlue($(this).text());
            }
        }

        if ($(this).parent()[0].className == 'aside_blue_tuo_ball') {
            if ($(this).hasClass("blue")) {
                $(this).removeClass("blue");
                blueArray_tuo.remove($(this).text());
            } else {
                $(this).addClass("blue");
                blueArray_tuo.push($(this).text());
                clearConflictDanBlue($(this).text());
            }
        }
    });
});

//清除所有普通投注红蓝球
clearCommonRedandBlue = function () {
   clearCommonRed();
   clearCommonBlue();
    // console.log(blueArray);
};

clearCommonRed = function () {
    blueNum = document.getElementsByClassName("article_red_ball")[0].getElementsByTagName("span");
    for (i = 0; i < blueNum.length; i++) {
        blueNum[i].classList.remove("red");
    }
    // console.log(redArray);
    redArray.length = 0;

};

clearCommonBlue = function () {
    leftNum = document.getElementsByClassName("aside_blue_ball")[0].getElementsByTagName("span");
    for (j = 0; j < leftNum.length; j++) {
        leftNum[j].classList.remove("blue");
    }

    blueArray.length = 0;
    // console.log(redArray);
    // console.log(blueArray);

}
//清除所有胆拖红蓝球
clearDanTuoRedandBlue = function () {
    blueNum = document.getElementsByClassName("article_red_dan_ball")[0].getElementsByTagName("span");
    for (var i = 0; i < blueNum.length; i++) {
        blueNum[i].classList.remove("red");
    }
    redArray_dan.length = 0;
    leftNum = document.getElementsByClassName("aside_blue_dan_ball")[0].getElementsByTagName("span");
    for (var i = 0; i < leftNum.length; i++) {
        leftNum[i].classList.remove("blue");
    }
    blueArray_dan.length =0;

    blueNum = document.getElementsByClassName("article_red_tuo_ball")[0].getElementsByTagName("span");
    for (var i = 0; i < blueNum.length; i++) {
        blueNum[i].classList.remove("red");
    }
    redArray_tuo.length = 0;
    leftNum = document.getElementsByClassName("aside_blue_tuo_ball")[0].getElementsByTagName("span");
    for (var i = 0; i < leftNum.length; i++) {
        leftNum[i].classList.remove("blue");
    }
    blueArray_tuo.length = 0;
};

//胆拖选择时，清除冲突对应的红球
clearConflictDanRed = function (val) {
    var leftNum = document.getElementsByClassName("article_red_dan_ball")[0].getElementsByTagName("span");
    for (var i = 0; i < leftNum.length; i++) {
        if (leftNum[i].innerHTML == val) {
            leftNum[i].classList.remove("red");
            redArray_dan.remove(val);
        }
    }
};

clearConflictTuoRed = function (val) {
    var leftNum = document.getElementsByClassName("article_red_tuo_ball")[0].getElementsByTagName("span");
    for (var i = 0; i < leftNum.length; i++) {
        if (leftNum[i].innerHTML === val) {
            leftNum[i].classList.remove("red");
            redArray_tuo.remove(val);
        }
    }
};

//胆拖选择时，清除冲突对应的蓝球
clearConflictDanBlue = function (val) {
    var leftNum = document.getElementsByClassName("aside_blue_dan_ball")[0].getElementsByTagName("span");
    for (var i = 0; i < leftNum.length; i++) {
        if (leftNum[i].innerHTML == val) {
            leftNum[i].classList.remove("blue");

            blueArray_dan.remove(val);
        }
    }
};

clearConflictTuoBlue = function (val) {
    var leftNum = document.getElementsByClassName("aside_blue_tuo_ball")[0].getElementsByTagName("span");
    for (var i = 0; i < leftNum.length; i++) if (leftNum[i].innerHTML === val) {
        leftNum[i].classList.remove("blue");

        blueArray_tuo.remove(val);
    }
};

//获取元素在数组的下标
Array.prototype.indexOf = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) {
            return i;
        }

    }
    return -1;
};
//根据数组的下标，删除该下标的元素
Array.prototype.remove = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

$("#btn_common_note").click(function () {
    if (redArray.length < 5) {
        alert('普通投注，红球数量必须大于等与5');
        return;
    }
    if (blueArray.length < 2) {
        alert('普通投注，蓝球数量必须大于等与2');
        return;
    }

    var textb = document.getElementById("text_common_phasenum");
    if (textb.value == '') {
        alert('请输入投注期数');
        return;
    } else {
        phase_num = textb.value;
    }

    $.ajax({
        url: "./common",
        type: "POST",
        data: {reda: redArray, bluea: blueArray, phasenum: phase_num},//期数为测试用，后期要补上，应该默认或自动补上
        success: function (result) {
            alert('提交成功');
        },
        error: function () {
            alert('提交失败');
        }

    });
});

$("#btn_dantuo_note").click(function () {
    var textb = document.getElementById("text_dantuo_phasenum");
    if (textb.value == '') {
        alert('请输入投注期数');
        return;
    } else {
        phase_num = textb.value;
    }

    if ((redArray_dan.length > 4) || (redArray_dan < 1)) {
        alert('红球胆码至少选择1个，最多可选4个');
        return;
    }

    if ((redArray_tuo < 1)) {
        alert('红球拖码至少选一个');
        return;
    }
    if (blueArray_dan.length > 1) {
        alert('蓝球胆码至多选一个');
        return;
    }

    if (blueArray_tuo.length < 1) {
        alert('蓝球拖码至少选一个');
        return;
    }

    if ((redArray_dan.length + redArray_tuo.length) < 5) {
        alert('红球总数必须大于等于5');
        return;
    }

    if ((blueArray_dan.length + blueArray_tuo.length) < 2) {
        alert('蓝球总数必须大于等于2');
        return;
    }

    $.ajax({
        url: "./dantuo",
        type: "POST",
        data: {
            reddan: redArray_dan,
            redtuo: redArray_tuo,
            bluedan: blueArray_dan,
            bluetuo: blueArray_tuo,
            phasenum: phase_num
        },//期数为测试用，后期要补上，应该默认或自动补上
        success: function (result) {
            alert('提交成功');
        },
        error: function () {
            alert('提交失败');
        }

    });
});

$("#btn_common_clear_all_select").click(function () {
   clearCommonRedandBlue();
});

$("#btn_dantuo_clear_all_select").click(function () {
   clearDanTuoRedandBlue();
});

$("#bn_red_clear").click(function () {
    clearCommonRed();
});

$("#bn_blue_clear").click(function () {
    clearCommonBlue();
});

//根据选择的随机红球数生成随机数
$("#bn_rnd_red").click(function () {
    clearCommonRed();
    rnd_red_num = $("#select_rnd_red option:selected").text();
    redArray = randomArray(rnd_red_num, 35);
    // console.log(redArray)
    blueNum = document.getElementsByClassName("article_red_ball")[0].getElementsByTagName("span");
    for (i = 1; i < blueNum.length; i++) {
       val = blueNum[i].innerHTML;
        if (redArray.indexOf(val) == -1) {
            continue;
        }
        blueNum[i].classList.add("red");
    }
});

//根据选择的随机蓝球数生成随机数
$("#bn_rnd_blue").click(function () {
    clearCommonBlue();
    rnd_blue_num = $("#select_rnd_blue option:selected").text();
    blueArray = randomArray(rnd_blue_num, 12);

    blueNum = document.getElementsByClassName("aside_blue_ball")[0].getElementsByTagName("span");
    for (i = 1; i < blueNum.length; i++) {
       val = blueNum[i].innerHTML;
        if (blueArray.indexOf(val) == -1) {
            continue;
        }
        blueNum[i].classList.add("blue");
    }
});

//根据输入的需要产生的随机数个数和最大数产生随机数
randomArray = function (rndnum, Maxnum) {
    var returnArray = [];
    while (returnArray.length < rndnum) {
        var value = rndone(Maxnum);
        if (returnArray.indexOf(value) == -1) {
            returnArray.push(value);
        }
    }
    return returnArray;
};

//根据输入的最大数，产生不超过最大数的随机数
rndone = function (Maxnum) {
    var res = parseInt(Math.random() * Maxnum) + 1;
    return res;
};